import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
      value: [],
      state: '0',
      isShow:false
  },
  mutations: {
    addtask(state, payload) {
      state.value.unshift({
        taskName: payload,
        state:false
      })
    },
    deltask(state,index) {
      state.value.splice(index,1)
    },
    setIsShow(state) {
      state.isShow = !state.isShow
      state.value.forEach(item=> item.state = !item.state)
    },
    setTaskState(state, index) {
      state.value[index].state = !state.value[index].state
    },
    delCompleteTask(state) {
      state.value = state.value.filter( item=> !item.state )
    },
    setState(state, val) {
      state.state = val
    },
  },
  getters: {
    taskNumber: (state) => state.value.length,
    stateList: (state) => {
      if (state.state === '-1') {
        return state.value.filter(item=> !item.state)
      }
      if (state.state === '1') {
        return state.value.filter(item=> item.state)
      }
      return state.value
    },
    state: (state) => state,
    isShow: (state) => {
      const show = state.value.every(item => item.state)
      if (show) {
        return true
      } else {
        return state.isShow
      }
    },
  },
  actions: {},
  modules: {}
})